﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Modules.Couple.BusinessEntities;

namespace StockholmsStad.Windows.UI.Modules.Couple.Models
{
    public class CeremonyPresentationModel : INotifyPropertyChanged
    {
        public event EventHandler<DataEventArgs<CeremonyPresentationModel>> Saved;
        public event EventHandler<DataEventArgs<bool>> Changed;
        public DelegateCommand<object> SaveCommand { get; private set; }

        #region Constructor

        public CeremonyPresentationModel()
        {
            SaveCommand = new DelegateCommand<object>(Save);
            PropertyChanged += OnPropertyChangedEvent;
        }

        #endregion

        #region Properties

        #region Common properties

        public string HeaderInfo
        {
            get { return HeaderNames.Ceremony; }
        }

        public int? TicketId { get; set; }

        private bool isInChangeMode;
        public bool IsInChangeMode
        {
            get
            {
                return isInChangeMode;
            }
            set
            {
                //if(isInChangeMode != value)
                //{
                    var changeHandler = Changed;

                    if (changeHandler != null)
                    {
                        var args = new DataEventArgs<bool>(value);
                        changeHandler(this, args);
                    }
                //}
                isInChangeMode = value;
            }
        }

        public List<ValueIdPair> CeremonyLanguages { get; set; }
        public List<ValueIdPair> CertificateLanguages { get; set; }

        private List<CeremonySpecialNeed> ceremonySpecialNeeds;
        public List<CeremonySpecialNeed> CeremonySpecialNeeds
        {
            get
            {
                return ceremonySpecialNeeds;
            }
            set
            {
                ceremonySpecialNeeds = value;
                PropertyChanged(this, new PropertyChangedEventArgs("CeremonySpecialNeeds"));
            }
        }

        private ValueIdPair certificateLanguage;
        public ValueIdPair CertificateLanguage
        {
            get
            {
                return certificateLanguage;
            }
            set
            {
                if (!value.Equals(certificateLanguage))
                {
                    certificateLanguage = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("CertificateLanguage"));
                }
            }
        }

        private ValueIdPair ceremonyLanguage;
        public ValueIdPair CeremonyLanguage
        {
            get
            {
                return ceremonyLanguage;
            }
            set
            {
                if (!value.Equals(ceremonyLanguage))
                {
                    ceremonyLanguage = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("CeremonyLanguage"));
                }
            }
        }

        private string otherLanguage;
        public string OtherLanguage
        {
            get
            {
                return otherLanguage;
            }
            set
            {
                if (!value.Equals(otherLanguage))
                {
                    otherLanguage = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("OtherLanguage"));
                }
            }
        }

        private bool interpreter;
        public bool Interpreter
        {
            get
            {
                return interpreter;
            }
            set
            {
                if (!value.Equals(interpreter))
                {
                    interpreter = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Interpreter"));
                }
            }
        }

        private bool hpHasArrived;
        public bool HPHasArrived
        {
            get
            {
                return hpHasArrived;
            }
            set
            {
                if (!value.Equals(hpHasArrived))
                {
                    hpHasArrived = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("HPHasArrived"));
                }
            }
        }

        private DateTime? hpIssuedDate;
        public DateTime? HPIssuedDate
        {
            get
            {
                return hpIssuedDate;
            }
            set
            {
                if (!value.Equals(hpIssuedDate))
                {
                    hpIssuedDate = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("HPIssuedDate"));
                }
            }
        }

        private DateTime? hpLatestDate;
        public DateTime? HPLatestDate
        {
            get
            {
                return hpLatestDate;
            }
            set
            {
                if (!value.Equals(hpLatestDate))
                {
                    hpLatestDate = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("HPLatestDate"));
                }
            }
        }

        private DateTime? hpEarliestDate;
        public DateTime? HPEarliestDate
        {
            get
            {
                return hpEarliestDate;
            }
            set
            {
                if (!value.Equals(hpEarliestDate))
                {
                    hpEarliestDate = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("HPEarliestDate"));
                }
            }
        }

        private string note;
        public string Note
        {
            get
            {
                return note;
            }
            set
            {
                if (!value.Equals(note))
                {
                    note = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Note"));
                }
            }
        }

        private string ministerFirstName;
        public string MinisterFirstName
        {
            get
            {
                return ministerFirstName;
            }
            set
            {
                if (!value.Equals(ministerFirstName))
                {
                    ministerFirstName = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("MinisterFirstName"));
                }
            }
        }
        private string ministerLastName;
        public string MinisterLastName
        {
            get
            {
                return ministerLastName;
            }
            set
            {
                if (!value.Equals(ministerLastName))
                {
                    ministerLastName = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("MinisterLastName"));
                }
            }
        }

        #endregion

        #endregion

        private void OnPropertyChangedEvent(object sender, PropertyChangedEventArgs e)
        {
            IsInChangeMode = true;
        }

        private void Save(object obj)
        {
            OnSaved(new DataEventArgs<CeremonyPresentationModel>(this));
        }

        private void OnSaved(DataEventArgs<CeremonyPresentationModel> e)
        {
            var savedHandler = Saved;

            if (savedHandler != null)
                savedHandler(this, e);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
